package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.ServiceObjectBean;
import com.sx.zongzhi.bean.ServiceSearchBean;
import com.sx.zongzhi.bean.ServicerBean;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 服务记录查询
 */
public class ServiceRecordSearchActivity extends BaseActivity {

    @BindView(R.id.et_ServiceLocal)
    EditText et_ServiceLocal;
    @BindView(R.id.tv_Worker)
    TextView tv_Worker;
    @BindView(R.id.et_OtherWorker)
    EditText et_OtherWorker;
    @BindView(R.id.tv_WorkerObject)
    TextView tv_WorkerObject;
    @BindView(R.id.tv_StartTime)
    TextView tv_StartTime;
    @BindView(R.id.tv_EndTime)
    TextView tv_EndTime;

    private Disposable disposable;
    /**服务人员*/
    private ServicerBean.RowsBean servicerBean;
    /**服务对象*/
    private ServiceObjectBean.RowsBean serviceObjBean;
    private String currentActivity="";

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_service_record_search);
        currentActivity=getIntent().getStringExtra(Global.ACTIVITY_FRAGMENT);
        setTitleName("服务记录查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        //订阅筛选组织机构
        disposable= MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if ("ServiceRecordSearchActivity".equals(bean.getRequest())) {
                           if (bean.getType()==0){//服务人员
                               servicerBean= (ServicerBean.RowsBean) bean.getMsg();
                               tv_Worker.setText(servicerBean.getMemberName());
                           }
                            if (bean.getType()==1){//服务对象
                                serviceObjBean= (ServiceObjectBean.RowsBean) bean.getMsg();
                                tv_WorkerObject.setText(serviceObjBean.getObjectName());
                            }
                        }
                    }
                });
    }

    @OnClick({R.id.tv_right,R.id.tv_Worker,R.id.tv_WorkerObject,R.id.tv_StartTime,R.id.tv_EndTime})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
            case R.id.tv_Worker:
                startActivity(new Intent(this,ServicerSelectActivity.class).
                        putExtra(Global.ACTIVITY_FRAGMENT,"ServiceRecordSearchActivity"));
                break;
            case R.id.tv_WorkerObject:
                startActivity(new Intent(this,ServiceObjectActivity.class).
                        putExtra(Global.ACTIVITY_FRAGMENT,"ServiceRecordSearchActivity"));
                break;
            case R.id.tv_StartTime:
                BaseUtil.createDataPop(this,tv_StartTime);
                break;
            case R.id.tv_EndTime:
                BaseUtil.createDataPop(this,tv_EndTime);
                break;


        }
    }

    /**提交查询*/
    private void submit() {
        String location=et_ServiceLocal.getText().toString().trim();
        String workedId="";
        if (servicerBean!=null){
            workedId=servicerBean.getMemberBaseId();
        }
        String otherWorker=et_OtherWorker.getText().toString().trim();
        String objectId="";
        if(serviceObjBean!=null){
            objectId=serviceObjBean.getObjectId();
        }
        String startTime=tv_StartTime.getText().toString();
        String endTime=tv_EndTime.getText().toString();
        ServiceSearchBean bean=new ServiceSearchBean(workedId,objectId,location,otherWorker,startTime,endTime);
        MyRxBus.get().post(new MsgEvent<>(currentActivity,1,bean));
        finish();
    }

    @Override
    protected void onDestroy() {
        if (disposable!=null&&disposable.isDisposed()){
            disposable.dispose();
        }
        super.onDestroy();
    }
}
